package com.wang.jmonkey.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.wang.jmonkey.modules.system.mapper.SystemModuleMapper;
import com.wang.jmonkey.modules.system.model.dto.SystemModuleDto;
import com.wang.jmonkey.modules.system.model.entity.SystemModuleEntity;
import com.wang.jmonkey.modules.system.model.entity.SystemResourceEntity;
import com.wang.jmonkey.modules.system.model.enums.SystemResourceTypeEnum;
import com.wang.jmonkey.modules.system.model.query.SystemModuleQuery;
import com.wang.jmonkey.modules.system.service.SystemModuleService;
import com.wang.jmonkey.modules.system.service.SystemResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 系统模块配置信息表(SystemModule)表服务实现类
 *
 * @author HeJiawang
 * @since 2021-05-21 21:45:20
 */
@Service
public class SystemModuleServiceImpl extends ServiceImpl<SystemModuleMapper, SystemModuleEntity> implements SystemModuleService {

    /**
     * mapper
     */
    @Autowired
    private SystemModuleMapper mapper;

    @Autowired
    private SystemResourceService systemResourceService;

    /**
     * 新增系统模块信息
     * @param entity 模块信息
     * @return true 新增成功
     */
    @Transactional
    @Override
    public boolean save(SystemModuleEntity entity) {
        super.save(entity);

        return systemResourceService.save(
                new SystemResourceEntity().setResourceId(entity.getId()).setType(SystemResourceTypeEnum.Module)
        );
    }

    /**
     * 分页查询
     * @param query 查询信息
     * @return 分页结果
     */
    @Override
    public PageInfo<SystemModuleEntity> page(SystemModuleQuery query) {
        Page<SystemModuleEntity> list = mapper.page(query);
        return new PageInfo<>(list);
    }

    /**
     * 获取所有系统模块信息
     * @return 系统模块信息
     */
    @Override
    public List<SystemModuleDto> listAll() {
        return mapper.listAll();
    }
}
